package org.example.struct.array;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2023/7/25 11:15
 * @apiNote
 */

public class Test00121MaxProfit {
    public static void main(String[] args) {
        int[] a = new int[]{1,4,0,9};
        int count = test(a);
        System.out.println("count:"+count+";array:" + JSON.toJSON(a));
    }

    /**
     *
     * @param prices
     * @return
     * 时间复杂度  O(N2) 空间复杂度 O(1）
     */
    public static int test(int[] prices) {
        int maxprofit = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                int profit = prices[j] - prices[i];
                maxprofit = Math.max(maxprofit, profit);
            }
        }
        return maxprofit;
    }

    public static int test1(int prices[]) {
        int minLeft = Integer.MAX_VALUE;
        int max = 0;
        for(int i =0;i<prices.length;i++){
            if(prices[i] < minLeft){
                minLeft = prices[i];
            }else if (prices[i] - minLeft > max){
                max = prices[i] - minLeft;
            }
        }
        return max;
    }
}
